/**
 * 
 */
package com.runningfox.humor;

import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * @author luhaiyang
 * 
 */
public class ListLayerCtrl implements OnTouchListener, 
		OnItemClickListener, OnItemSelectedListener {
	public static final int LAYER_ALL = 0;
	public static final int LAYER_UNREAD = 1;
	public static final int LAYER_FAVOURITE = 2;
	public static final int LAYER_HISTORY = 3;
	public static int curLayer =LAYER_ALL; 
	Humor callback;
	public float px = -1, py = -1;
	ListView list;
	JokeListAdapter jokeListAdapter =null;
	
	static final int ALL = 0;
	static final int UNREAD = 1;
	static final int FAVOURITE = 2;
	static final int SHARE = 3;
	
	
	public ListLayerCtrl(JokeListAdapter jokeListAdapter) {
		
	}

	/**
	 * @param callback
	 */
	public ListLayerCtrl(Humor callback,JokeListAdapter jokeListAdapter) {
		super();
		this.callback = callback;
		list = callback.jokeList;
		this.jokeListAdapter = jokeListAdapter;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnTouchListener#onTouch(android.view.View,
	 * android.view.MotionEvent)
	 */
	@Override
	public boolean onTouch(View view, MotionEvent event) {
		// TODO Auto-generated method stub
		int action = event.getAction();
		ListView list = callback.jokeList;
		int first = list.getFirstVisiblePosition();
		int last = list.getLastVisiblePosition();
		switch (action) {
		case MotionEvent.ACTION_UP: {
			callback.actionHideStatus();
			float offset = event.getY() - py;
			if (first == 0 && offset > 50) {
				// callback.actionShowTopBtns();
			}
			if (offset < -50) {
				// callback.actionHideTopBtns();
				if (last == list.getCount() - 1) {
					callback.actionLoadMoreJokes();
				}
			}
			px = -1;
			py = -1;
//			callback.ad.setVisibility(View.VISIBLE);
			break;
		}
		case MotionEvent.ACTION_DOWN: {
			if (py == -1) {
				py = event.getY();
			}
//			callback.ad.setVisibility(View.GONE);
			break;
		}
		}
		return false;
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		JokeNodeView v = (JokeNodeView) view;
//		if(v.node.isSelected){
		callback.actionLoadJoke(v.node);
//		}else if (!v.node.isSelected) {
//			this.jokeListAdapter.setSelected(position);
//			v.setExpended(true);
//		}
//		if(parent!=null){
//			parent.invalidate();
//		}
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		Log.v("debug", "onlist selcted");
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		Log.v("debug", "onlist nothingselected");
	}
}
